package com.augurit.wf.bi.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.sysmeta.service.ISysMetaColumnService;
import com.augurit.common.sysmeta.service.ISysMetaTableService;
import com.augurit.common.sysmeta.web.form.SysMetaColumnForm;
import com.augurit.common.sysmeta.web.form.SysMetaTableForm;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.common.extmodel.ImportFieldCheckTreeNode;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "wf-bus-template.action", type = "redirect"),
	@Result(name = WfBusTemplateAction.SUCCESS, location = "/wf/bi/bus-template/import-fields.jsp")
})
public class WfImportFieldAction extends ExtBaseAction {
	
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	
	@Autowired
	private ISysMetaColumnService sysMetaColumnService;
	
	private List<String> fieldsAlreadyBeImported;		//模板中已经存在的页面元素
	private List<String> fieldsBeCheckedFromPage;			//前台页面被打钩、需要被导入的字段
	private List<String> fieldNamesOfBeChecked;				//前台页面被打钩、需要被导入的字段对应的名称
	
	private Long templateId;
	
	/**
	 * 获取业务表字段列表
	 * @throws Exception
	 */
	public String getBusTableFields()throws Exception {
		WfBusTemplateForm form = wfBusTemplateService.get(templateId);
		
		//获取业务表
		List<SysMetaTableForm> tables = wfBusTemplateService.getRelateEntities(templateId);
		List<ImportFieldCheckTreeNode> nodes = null;
		
		if(tables != null && tables.size() > 0){
			//获取已经导入的字段
			fieldsAlreadyBeImported = wfBusTemplateService.getFieldTypeElementIdInTemplate(templateId);
			
			nodes = new ArrayList<ImportFieldCheckTreeNode>();
			//对于每一个表
			for(SysMetaTableForm metaTableForm:tables){
				ImportFieldCheckTreeNode node = ImportFieldCheckTreeNode.convertToColumnTreeNode(metaTableForm);
				
				//获取表中的字段
				List<SysMetaColumnForm> columns = sysMetaColumnService.getColumnsByTableId(metaTableForm.getId());
				List<ImportFieldCheckTreeNode> children = ImportFieldCheckTreeNode.convertColumnsToColumnTreeNodeList(form.getTemplateCode(), fieldsAlreadyBeImported, columns, metaTableForm.getEntityName());
				node.setChildren(children);
				if(columns == null)
					node.setLeaf(true);
				nodes.add(node);
			}
		}
		
		extRenderListJson(nodes, false);
		return null;
	}
	
	/**
	 * 通过比较已导入的字段和页面上打钩的字段，找出页面上已打钩但是模板中还没有导入的字段
	 * @return
	 */
	private List<String> getFieldsNeededToBeImported(){
		if(fieldsBeCheckedFromPage != null && fieldsBeCheckedFromPage.size() > 0){
			List<String> filedsNeedToBeImported = new ArrayList<String>(fieldsBeCheckedFromPage);
			
			if(fieldsAlreadyBeImported != null && fieldsAlreadyBeImported.size() > 0){
				filedsNeedToBeImported.removeAll(fieldsAlreadyBeImported);
			}
			return filedsNeedToBeImported;
		}
		return null;
	}
	
	/**
	 * 通过比较已导入的字段和页面上打钩的字段，找出模板中已导入但是被页面取消导入的字段
	 * @return
	 */
	private List<String> getFieldsNeedToBeDeleted(){
		if(fieldsAlreadyBeImported != null && fieldsAlreadyBeImported.size() > 0){
			List<String> fieldNeededToBeDeleted = new ArrayList<String>(fieldsAlreadyBeImported);
			
			if(fieldsBeCheckedFromPage != null && fieldsBeCheckedFromPage.size() > 0){
				fieldNeededToBeDeleted.removeAll(fieldsBeCheckedFromPage);
			}
			return fieldNeededToBeDeleted;
		}
		return null;
	}
	
	/**
	 * 导入字段页面元素
	 * @return
	 * @throws Exception
	 */
	public String importFields() throws Exception{
		Map<String,String> fieldsComments = new HashMap<String,String>();
		if(fieldsBeCheckedFromPage != null && fieldsBeCheckedFromPage.size() > 0){
			for(int i=0; i <  fieldsBeCheckedFromPage.size(); i++){
				fieldsComments.put(fieldsBeCheckedFromPage.get(i), fieldNamesOfBeChecked.get(i));
			}
		}
		
		List<String> filedsNeedToBeImported = getFieldsNeededToBeImported();
		if(filedsNeedToBeImported != null && filedsNeedToBeImported.size() > 0){
			wfBusTemplateService.importFieldsToBusTemplate(templateId, filedsNeedToBeImported, fieldsComments);
		}
		
		List<String> fieldsNeededToBeDeleted = getFieldsNeedToBeDeleted();
		if(fieldsNeededToBeDeleted != null && fieldsNeededToBeDeleted.size() > 0) {
			wfBusTemplateService.deleteFieldsFromBusTemplate(templateId, fieldsNeededToBeDeleted);
		}
		return null;
	}
	
	/**
	 * 获取导入字段check树
	 * @return
	 * @throws Exception
	 */
	public String getImportFieldTree() throws Exception{
		fieldsAlreadyBeImported = wfBusTemplateService.getFieldTypeElementIdInTemplate(templateId);
		return SUCCESS;
	}

	
	public Long getTemplateId() {
		return templateId;
	}

	public void setTemplateId(Long templateId) {
		this.templateId = templateId;
	}

	public List<String> getFieldsAlreadyBeImported() {
		return fieldsAlreadyBeImported;
	}

	public void setFieldsAlreadyBeImported(List<String> fieldsAlreadyBeImported) {
		this.fieldsAlreadyBeImported = fieldsAlreadyBeImported;
	}

	public List<String> getFieldsBeCheckedFromPage() {
		return fieldsBeCheckedFromPage;
	}

	public void setFieldsBeCheckedFromPage(List<String> fieldsBeCheckedFromPage) {
		this.fieldsBeCheckedFromPage = fieldsBeCheckedFromPage;
	}

	public List<String> getFieldNamesOfBeChecked() {
		return fieldNamesOfBeChecked;
	}

	public void setFieldNamesOfBeChecked(List<String> fieldNamesOfBeChecked) {
		this.fieldNamesOfBeChecked = fieldNamesOfBeChecked;
	}

	
	
}
